﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Maspaint : MonoBehaviour
{
    private LineRenderer CurrentLine;  
    private Vector3 lastPosition;
    private bool isMouseDown = false;  
    private float waitTime = 0.05f;//从什么时候开始去点
    private List<Vector3> positions = new List<Vector3>();

    public Material lindermaterial;

    //private PolygonCollider2D polygonCollider;//设置多边形碰撞器

    private void Awake()
    {

       // polygonCollider = GetComponent<PolygonCollider2D>();      
        //polygonCollider.enabled = false;
    }
    private void Start()
    {
        CurrentLine = GetComponent<LineRenderer>();
        CurrentLine.material = lindermaterial;
        CurrentLine.startWidth = 0.2f;//设置划线的初始宽度
        CurrentLine.endWidth = 0.2f;//设置划线结束宽度
        CurrentLine.startColor = Color.black;//设置划线初始颜色
        CurrentLine.endColor = Color.black;//设置划线结束颜色
        CurrentLine.numCornerVertices = 20;//设置转角处的圆润程度
        CurrentLine.numCapVertices = 5;//设置结束处的圆润程度
    }
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //if (EventSystem.current.IsPointerOverGameObject() == false)//判断是否点击再来UI上
            //{              
            //    isMouseDown = true;                          
            //    Vector3 position = GetMousePoint();
            //    AddPosition(position);
            //}
            isMouseDown = true;
            Vector3 position = GetMousePoint();
            AddPosition(position);
        }
        if (isMouseDown)
        {
            if (positions.Count == 1)
            {
                waitTime = 0.2f;
            }
            else if (positions.Count > 1)
            {
                waitTime -= Time.deltaTime;
                if (waitTime <= 0)
                {
                    positions.RemoveAt(0);
                    CurrentLine.positionCount = positions.Count;
                    CurrentLine.SetPositions(positions.ToArray());//把list放进LineRenderer中

                    //创建碰撞路径
                    //polygonCollider.enabled = true;
                    //List<Vector2> colliderPath = GetColliderPath(positions);
                    //polygonCollider.SetPath(0, colliderPath.ToArray());
                }
            }
            Vector3 position = GetMousePoint();
            if (Vector3.Distance(position, lastPosition) > 0.1f)
            {              
                AddPosition(position);
            }
        }
        if (Input.GetMouseButtonUp(0))
        {         
            isMouseDown = false;
            //polygonCollider.enabled = false;
            positions.Clear();
            CurrentLine.positionCount = positions.Count;
            CurrentLine.SetPositions(positions.ToArray());//把list放进LineRenderer中

            //创建碰撞路径          
            //List<Vector2> colliderPath = GetColliderPath(positions);
            //polygonCollider.SetPath(0, colliderPath.ToArray());
        }
    }

    void AddPosition(Vector3 position)
    {      
        positions.Add(position);
        CurrentLine.positionCount = positions.Count;
        CurrentLine.SetPositions(positions.ToArray());
        lastPosition = position;

        //创建碰撞路径
        // List<Vector2> colliderPath = GetColliderPath(positions);
        //polygonCollider.SetPath(0, colliderPath.ToArray());
    }
    private Vector3 GetMousePoint()
    {
       
        Vector3 mousepoint = Vector3.zero;

        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
        if (hit.collider != null)
        {           
            mousepoint = (Vector3)hit.point;
            mousepoint.z -= 0.2f;
            return mousepoint;
        }
        return Vector3.zero;
    }

    //计算碰撞体轮廓
   // float colliderWidth;
   // List<Vector2> pointList2 = new List<Vector2>();
    //List<Vector2> GetColliderPath(List<Vector3> pointList3)
    //{
        //碰撞体宽度
        //colliderWidth = CurrentLine.startWidth;
        //Vector3转Vector2
       // pointList2.Clear();
        //for (int i = 0; i < pointList3.Count; i++)
        //{
           // pointList2.Add(pointList3[i]);
        //}

        //碰撞体轮廓点位
        //List<Vector2> edgePointList = new List<Vector2>();
        //以LineRenderer的点位为中心, 沿法线方向与法线反方向各偏移一定距离, 形成一个闭合且不交叉的折线
       // for (int j = 1; j < pointList2.Count; j++)
        //{
            //当前点指向前一点的向量
            //Vector2 distanceVector = pointList2[j - 1] - pointList2[j];
            //法线向量
            //Vector3 crossVector = Vector3.Cross(distanceVector, Vector3.forward);
            //标准化, 单位向量
            //Vector2 offectVector = crossVector.normalized;
            //沿法线方向与法线反方向各偏移一定距离
            //Vector2 up = pointList2[j - 1] + 0.5f * colliderWidth * offectVector;
            //Vector2 down = pointList2[j - 1] - 0.5f * colliderWidth * offectVector;
            //分别加到List的首位和末尾, 保证List中的点位可以围成一个闭合且不交叉的折线
            //edgePointList.Insert(0, down);
            //edgePointList.Add(up);
            ////加入最后一点
            //if (j == pointList2.Count - 1)
            //{
            //    up = pointList2[j] + 0.5f * colliderWidth * offectVector;
            //    down = pointList2[j] - 0.5f * colliderWidth * offectVector;
            //    edgePointList.Insert(0, down);
            //    edgePointList.Add(up);
           // }
       // }
        //返回点位
       // return edgePointList;
    //}
  
}
